package com.xdf.ecommerce.partition;

import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.stream.binder.PartitionSelectorStrategy;
import org.springframework.stereotype.Component;

/**
 * 为发送消息自定义分区策略
 * @author chanchaw
 * @create 2025-11-11 10:01
 */
@Slf4j
@Component
public class QinyiPartitionSelectorStrategy implements PartitionSelectorStrategy {
    @Override
    public int selectPartition(Object key, int partitionCount) {
        String keyStr = key.toString();
        int partition = keyStr.hashCode() % partitionCount;
        log.info("为发送消息自定义分区策略时 key:[{}], partitionCount:[{}], partition:[{}]", keyStr, partitionCount, partition);
        return partition;
    }
}
